package idao;

import com.mysql.cj.QueryResult;
import dao.Conn;
import dao.IStudentImpl;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import pojo.TbStudent;

import java.sql.Connection;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

public class IStudentDao implements IStudentImpl {
    //添加
    @Override
    public int insert(TbStudent stu) throws Exception {
        String sql = "insert into tb_student(id,name,age,gender,tel,class_id) values(null,?,?,?,?,?)";
        Object[] params = {stu.getName(), stu.getAge(), stu.getGender(), stu.getTel(), stu.getClass_id()};
        Connection conn = Conn.getConn();
        //连接池优化了代码的增删改查
        QueryRunner queryRunner = new QueryRunner();
        int cnn = queryRunner.update(conn, sql, params);
        System.out.println(sql);
        return cnn;
    }

    @Override
    public int delete(int id) throws Exception {
        String sql = "delete from tb_student where id = ?";
        Object[] params = {id};
        //建立连接数据库
        Connection conn = Conn.getConn();
        QueryRunner queryRunner = new QueryRunner();
        int cnn = queryRunner.update(conn, sql, params);
        System.out.println(sql);
        return cnn;
    }

    @Override
    public int update(TbStudent stu) throws Exception {
        String sql = "update tb_student set ";
        List<Object> params = new ArrayList<>();
        if (stu != null) {
            boolean b = false;
            if (stu.getName() != null) {
                if (b) {
                    sql += " ,name=?";
                } else {
                    sql += " name=?";
                }
                b = true;
                params.add(stu.getName());
            }

            if (stu.getAge() != null) {
                if (b) {
                    sql += " ,age=?";
                } else {
                    sql += " age=?";
                }
                b = true;
                params.add(stu.getAge());
            }

            if (stu.getGender() != null) {
                if (b) {
                    sql += " ,gender=?";
                } else {
                    sql += " gender=?";
                }
                b = true;
                params.add(stu.getGender());
            }

            if (stu.getTel() != null) {
                if (b) {
                    sql += " ,tel=?";
                } else {
                    sql += " tel=?";
                }
                b = true;
                params.add(stu.getTel());
            }

            if (stu.getClass_id() != null) {
                if (b) {
                    sql += " ,class_id=?";
                } else {
                    sql += " class_id=?";
                }
                b = true;
                params.add(stu.getClass_id());
            }
        }
        sql+=" where id=?";
        params.add(stu.getId());

        Object[] arr = new Object[params.size()];
        params.toArray(arr);

        Connection conn = Conn.getConn();
        QueryRunner qr = new QueryRunner();
        int n = qr.update(conn, sql, arr);
        System.out.println(sql);
        return n;
    }

    @Override
    public TbStudent getStudentById(int id) throws Exception {
        String sql = "select * from tb_student where id= ?";
        Object[] params = {id};
        Connection conn = Conn.getConn();
        QueryRunner queryRunner = new QueryRunner();
        TbStudent cnn = queryRunner.query(conn, sql, new BeanHandler<TbStudent>(TbStudent.class), params);
        return cnn;
    }

    @Override
    public int getCount() {
        return 0;
    }

    @Override
    public List<TbStudent> getStudent(TbStudent stu) {
        return null;
    }

    @Override
    public List<Map<String, Object>> getStudent_classNmae_teacherName(TbStudent stu) {
        return null;
    }
}
